package com.ljz.servlet;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.ljz.dao.EmpDao;
import com.ljz.entity.Deptme;
import com.ljz.entity.Employ;
import com.ljz.utils.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/getemps")
public class EmployInforServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        EmpDao empDao = sqlSession.getMapper(EmpDao.class);
        resp.setContentType("application/json;charset=utf-8");

//        Double salary = Double.parseDouble(req.getParameter("salary"));
//        Integer deptId = Integer.parseInt(req.getParameter("deptId"));
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        PageHelper.startPage(currentPage,pageSize);
        Employ emp = new Employ();
        Deptme deptme = new Deptme();
//        deptme.setDeptid(deptId);
//        emp.setSalary(salary);
        emp.setDeptme(deptme);
        List<Employ> list = empDao.findEnameByDeptIdAndSal(emp);
        PageInfo<Employ> pageInfo = new PageInfo<>(list);
        sqlSession.close();
        Gson gson = new Gson();
        String json = gson.toJson(pageInfo);
        PrintWriter out = resp.getWriter();
        out.print(json);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
